package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mydy.model.RewardTask;
import com.example.mydy.utils.MockDataManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 提交悬赏任务完成证明的页面
 */
public class SubmitRewardTaskActivity extends AppCompatActivity {
    
    private TextView tvTaskTitle;
    private TextView tvTaskDescription;
    private TextView tvTaskReward;
    private EditText etSubmissionContent;
    private Button btnSubmit;
    private Button btnCancel;
    
    private int taskId;
    private RewardTask currentTask;
    private MockDataManager mockDataManager;
    private String currentUsername;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_reward_task);
        
        // 初始化视图和数据
        initViews();
        initData();
    }
    
    private void initViews() {
        tvTaskTitle = findViewById(R.id.tv_task_title);
        tvTaskDescription = findViewById(R.id.tv_task_description);
        tvTaskReward = findViewById(R.id.tv_task_reward);
        etSubmissionContent = findViewById(R.id.et_submission_content);
        btnSubmit = findViewById(R.id.btn_submit);
        btnCancel = findViewById(R.id.btn_cancel);
        
        // 设置返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
        
        // 设置取消按钮
        btnCancel.setOnClickListener(v -> finish());
        
        // 设置提交按钮
        btnSubmit.setOnClickListener(v -> submitTask());
    }
    
    private void initData() {
        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUsername();
        
        // 获取任务ID
        Intent intent = getIntent();
        if (intent != null) {
            taskId = intent.getIntExtra("task_id", -1);
            if (taskId == -1) {
                Toast.makeText(this, "任务数据错误", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
            
            // 加载任务数据
            loadTaskData();
        }
    }
    
    private void loadTaskData() {
        // 获取任务详情
        RewardTask task = null;
        for (RewardTask t : mockDataManager.getAllRewardTasks()) {
            if (t.getId() == taskId) {
                task = t;
                break;
            }
        }
        
        if (task == null) {
            Toast.makeText(this, "未找到相关任务信息", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        currentTask = task;
        
        // 设置任务基本信息
        tvTaskTitle.setText(task.getTitle());
        tvTaskDescription.setText(task.getDescription());
        tvTaskReward.setText(String.format(Locale.CHINA, "奖励: ¥%.2f", task.getReward()));
        
        // 如果任务已经提交过，显示之前的提交内容
        if (task.getSubmissionContent() != null && !task.getSubmissionContent().isEmpty()) {
            etSubmissionContent.setText(task.getSubmissionContent());
        }
        
        // 如果任务已经审核通过或拒绝，禁用提交按钮
        if (task.getProgressStatus() == RewardTask.STATUS_COMPLETED || 
            task.getProgressStatus() == RewardTask.STATUS_REJECTED) {
            etSubmissionContent.setEnabled(false);
            btnSubmit.setEnabled(false);
        }
    }
    
    private void submitTask() {
        // 获取提交内容
        String submissionContent = etSubmissionContent.getText().toString().trim();
        
        // 验证提交内容
        if (TextUtils.isEmpty(submissionContent)) {
            Toast.makeText(this, "请输入提交内容", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 确认对话框
        new AlertDialog.Builder(this)
            .setTitle("提交确认")
            .setMessage("确定要提交任务完成证明吗？提交后将等待管理员审核")
            .setPositiveButton("确定", (dialog, which) -> {
                // 更新任务状态
                currentTask.setSubmissionContent(submissionContent);
                currentTask.setSubmissionTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault()).format(new Date()));
                currentTask.setProgressStatus(RewardTask.STATUS_UNDER_REVIEW); // 更新为待审核状态
                
                // 保存更新
                boolean success = mockDataManager.updateRewardTask(currentTask);
                if (success) {
                    Toast.makeText(this, "提交成功，等待审核", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                } else {
                    Toast.makeText(this, "提交失败，请重试", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("取消", null)
            .show();
    }
} 